using System;
using System.Collections.Generic;
using System.Text;
using Cit.CDE.Net.Module;

namespace UI.UserControl
{
    public class MultipleSelect
    {
        public static string getCheckBoxs(string name, List<CDECandidate> listCandidate, string defaultValue, string style)
        {
            return getCheckBoxs(name, listCandidate, defaultValue, style, "");
        }

        public static string getCheckBoxs(string name, List<CDECandidate> listCandidate, string defaultValue, string style, string css)
        {
            if(listCandidate == null) return "&nbsp;";

            string sstyle = "";
            string scss = "";
            if (!"".Equals(style))
            {
                sstyle = " style=\"" + style + "\"";
            }
            if (!"".Equals(style))
            {
                scss = " class=\"" + css + "\"";
            }

            string check = "";
            string name_id = "";
            StringBuilder sb = new StringBuilder();

            int i = 0;
            foreach (CDECandidate cd in listCandidate)
            {
                if (defaultValue == null)
                {
                    check = "";
                }
                else if (cd.Value.Equals(defaultValue)
                    || defaultValue.IndexOf(cd.Value + ",") == 0
                    || defaultValue.IndexOf("," + cd.Value + ",") > -1
                    || (defaultValue.LastIndexOf(",") > -1 && defaultValue.LastIndexOf(",") == defaultValue.IndexOf("," + cd.Value)))
                {
                    check = " checked";
                }
                else
                {
                    check = "";
                }
                name_id = name + "_" + i.ToString();

                //id name
                sb.Append("&nbsp;<input type=\"checkbox\" name=\"").Append(name).Append("\" id=\"").Append(name_id).Append("\"");

                //checked
                sb.Append(check);

                //style class 
                sb.Append(style).Append(css);

                //value
                sb.Append(" value=\"").Append(cd.Value).Append("\"").Append("\" />");

                //label
                sb.Append("<label for=\"").Append(name_id).Append("\"").Append(style).Append(css).Append(">").Append(cd.Label).Append("</label>");

                i++;
            }
            return sb.ToString();
        }

        public static string getRadioBoxs(string name, List<CDECandidate> listCandidate, string defaultValue, string style)
        {
            return getRadioBoxs(name, listCandidate, defaultValue, style, "");
        }

        public static string getRadioBoxs(string name, List<CDECandidate> listCandidate, string defaultValue, string style, string css)
        {
            if (listCandidate == null) return "&nbsp;";

            string sstyle = "";
            string scss = "";
            if (!"".Equals(style))
            {
                sstyle = " style=\"" + style + "\"";
            }
            if (!"".Equals(style))
            {
                scss = " class=\"" + css + "\"";
            }

            int i = 0;
            string check = "";
            string name_id = "";
            StringBuilder sb = new StringBuilder();

            foreach (CDECandidate cd in listCandidate)
            {
                if (cd.Value.Equals(defaultValue))
                {
                    check = " checked";
                }
                else
                {
                    check = "";
                }
                name_id = name + "_" + i.ToString();

                //id name
                sb.Append("&nbsp;<input type=\"radio\" name=\"").Append(name).Append("\" id=\"").Append(name_id).Append("\"");

                //checked
                sb.Append(check);

                //style class 
                sb.Append(style).Append(css);

                //value
                sb.Append(" value=\"").Append(cd.Value).Append("\"").Append("\" />");

                //label
                sb.Append("<label for=\"").Append(name_id).Append("\"").Append(style).Append(css).Append(">").Append(cd.Label).Append("</label>");

                i++;
            }
            return sb.ToString();
        }
    }
}
